Here is a program in Java that sums up all array element and print out the result to the console.
class Main { public static void main(String[] args) { int[] arr = {1,2,3,4,5}; int total = 0; for (int i = 0; i < arr.length; i++) total += arr[i]; System.out.printf("The sum of array elements is: %d%n", total); } }
Note: the program will throw the error “incompatible types: String cannot be converted to int” if the array contains elements that are not summable, such as string elements
Comments